package com.srwl.mytx.adapter;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.ChatRoomActivity;
import com.srwl.mytx.activity.LiveRoomSearchActivity;
import com.srwl.mytx.domain.LiveRoom;
import com.srwl.mytx.utils.ClickUtil;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LiveRoomListAdapter extends RecyclerView.Adapter<LiveRoomListAdapter.PhotoViewHolder> {
    private static final String TAG = "LiveRoomListAdapter";
    private static final int REQUESTCODE_EDIT_TOPIC = 1;
    private final List<LiveRoom> liveRoomList;
    private final Context context;
    private int density;
    private Fragment fragment;

    public LiveRoomListAdapter(Context context, Fragment fragment, List<LiveRoom> liveRoomList) {
        this.liveRoomList = liveRoomList;
        this.context = context;
        this.fragment = fragment;
        density = (int) context.getResources().getDisplayMetrics().density;
    }

    public LiveRoomListAdapter(Context context, List<LiveRoom> liveRoomList) {
        this.liveRoomList = liveRoomList;
        this.context = context;
        density = (int) context.getResources().getDisplayMetrics().density;
    }

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final PhotoViewHolder holder = new PhotoViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_livelist_item, parent, false));


        //  holder.imageView.setOnTouchListener();
        return holder;
    }

    @Override
    public void onBindViewHolder(final PhotoViewHolder holder, final int position) {
        final GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        if (position < 2) {
            switch (position) {
                case 0:
                    layoutParams.rightMargin = 1 * density;
                    break;
                case 1:
                    layoutParams.leftMargin = 1 * density;
            }
        } else {
            switch (position % 2) {
                case 0:
                    layoutParams.rightMargin = 1 * density;
                    break;
                case 1:
                    layoutParams.leftMargin = 1 * density;
            }
        }
        layoutParams.bottomMargin = 2 * density;
        LiveRoom liveRoom = liveRoomList.get(position);
        String title = liveRoom.getTitle();
        if (liveRoom.getStatus() == Constant.TOPIC_STATUS_WAIT_VERIFY) {
            title = "[审核中] " + title;
//            holder.tv_desc.setVisibility(View.VISIBLE);
//            holder.tv_desc.setText("审核中不能正常搜索和使用");
//            holder.tv_desc.setTextColor(context.getResources().getColor(R.color.holo_red_light));
        } else {
            holder.tv_desc.setVisibility(View.GONE);
        }
        holder.tv_title.setText(title);
        Integer audienceSize = liveRoom.getAudienceSize();
        if (audienceSize == null) {
            audienceSize = 0;
        }
        holder.audienceNum.setText(audienceSize + "人");

        String scale50Url = Utils.getLiveRoomListAdapterUrl(liveRoom);
        Log.i(TAG, "onBindViewHolder: scale50Url" + scale50Url);
//        Glide.with(context)
//                .load(scale50Url)
//                // .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .into(holder.imageView);
        GlideLoadUtils.glideLoad(context,scale50Url,holder.imageView);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ClickUtil.isFastClick()) return;
                //判断是否为搜索聊天室列表
                if (context instanceof LiveRoomSearchActivity) {
                    ((Activity) context).startActivityForResult(new Intent(context, ChatRoomActivity.class).putExtra(Constant.EXTRA_INFO_LIVE_ROOM, liveRoom), REQUESTCODE_EDIT_TOPIC);
                } else {
                    fragment.startActivityForResult(new Intent(context, ChatRoomActivity.class).putExtra(Constant.EXTRA_INFO_LIVE_ROOM, liveRoom), REQUESTCODE_EDIT_TOPIC);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return liveRoomList.size();
    }


    class PhotoViewHolder extends RecyclerView.ViewHolder {
        // 替代findViewById的一个写法
        @BindView(R.id.photo)
        ImageView imageView;
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.audience_num)
        TextView audienceNum;
        @BindView(R.id.tv_desc)
        TextView tv_desc;

        public PhotoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            //  anchor= (TextView) itemView.findViewById(R.id.author);
        }
    }


}